//
//  HomeViewController.m
//  KMedicine
//
//  Created by 曹牧 on 16/7/5.
//  Copyright © 2016年 caomu. All rights reserved.
//
//  首页

#import "HomeViewController.h"
#import "AkuWeatherModel.h"
#import "AkuWeaTherView.h"
#import "AkuSelectCityController.h"
#import "CenterViewContro.h"
#import "AkuBottomView.h"
#import "AkuCitysModel.h"
#import "DiseaseViewController.h"
#import "PublicServiceViewController.h"
#import "WelfareViewController.h"
@interface HomeViewController ()


@property (nonatomic, strong) AkuWeatherModel *weatherModel;

// 天气view
@property (nonatomic, weak) AkuWeaTherView *weatherView;

// 图片轮播
@property (nonatomic, strong) CenterViewContro *scrollView;

// 名医通
@property (nonatomic, weak) UIButton *superDoctorView;


// 底部 6 个 view 的 父view
@property (nonatomic, weak) UIView *buttomView;

// 底部 6 个 view 的 数据源
@property (nonatomic, strong) NSArray *bottomArray;

@end

@implementation HomeViewController


- (void)setupUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.barTintColor = kThemeColor;
    
    self.title = @"马医一生";
}


- (void)viewDidLoad {
    
    
    // 打开网络监测
//    [self openOnitorNetWorkDiagonse];
    
    
    [super viewDidLoad];
    
    [self setupUI];
    
    //MARK: - 天气模块
    [self setWeaTherView];
    
    //MARK: - 图片轮播
    [self setScrollImg];
    
    //MARK: - 名医通 界面
    [self setSuperDoctorView];
    
    
    //MARK: - 底部的 6 个 img
    [self setBottomViews];
    
    //MARK: - 设置 模块的 约束
    [self setViewsMasonry];
}


#pragma mark - 天气模块
- (void)setWeaTherView {
    
    AkuWeaTherView *weatherView = [[AkuWeaTherView alloc] init];
    
    self.weatherView = weatherView;
    
    [self.view addSubview:weatherView];
    

    __weak typeof(self) weakSelf = self;
    
    // 自定义 天气 view  点击 选择 城市 的 回调
    weatherView.didPictCityButton = ^{
        
        // push
        AkuSelectCityController *selectCityCon = [[AkuSelectCityController alloc] initWithCityArr:[AkuCitysModel provsesAndCityArr]];
        
        // 设置 标题
        selectCityCon.title = @"选择当前省会位置";
        
        selectCityCon.backSelectCity = ^(NSString *city) {
            NSLog(@"%@", city);
            
            // 从新去 模型取数据
            [AkuWeatherModel weatherWithCurrentCity:city finishBlock:^(AkuWeatherModel *weatherModel) {
                NSLog(@"===>%@", weatherModel.currentCity);
                weakSelf.weatherView.weatherModel = weatherModel;
                
            } errorBlock:^{
                
                NSLog(@"网络有问题 获取不到 城市");
            }];
        };
        
        [self.navigationController pushViewController:selectCityCon animated:YES];
    };
    
    [AkuWeatherModel weatherWithCurrentCity:@"上海" finishBlock:^(AkuWeatherModel *weatherModel) {
        
        weatherView.weatherModel = weatherModel;
        
    } errorBlock:^{
        
        NSLog(@"网络有问题 获取不到 城市");
    }];
    
}

#pragma mark - 图片轮播
- (void)setScrollImg {
    
    CenterViewContro *scrollView = [[CenterViewContro alloc] init];
    
    self.scrollView = scrollView;
    
    [self.view addSubview:scrollView.view];
}

#pragma mark - 名医通
- (void)setSuperDoctorView {
    
    UIButton *superDoctor = [[UIButton alloc] init];
    
    self.superDoctorView = superDoctor;
    
    superDoctor.userInteractionEnabled = NO;
    
    [superDoctor setImage:[UIImage imageNamed:@"yj"] forState:UIControlStateNormal];
    
    [superDoctor setTitle:@"名医通" forState:UIControlStateNormal];
    
    [superDoctor setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    // button 图片 文字   的内边距
    superDoctor.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0 );
    superDoctor.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0 );
    
    // button 内 控件的 对齐方式
    superDoctor.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    [self.view addSubview:superDoctor];
    
}

#pragma mark - 创建 底部的 6 个 view
- (void)setBottomViews {
    
    UIView *bottomView = [[UIView alloc] init];
    
    self.buttomView = bottomView;
    
    [self.view addSubview:bottomView];
    
    // 添加 里面 的 6个 button
    for (int i = 0; i < self.bottomArray.count / 2; i++) {
        
        AkuBottomView *botView = [[AkuBottomView alloc] initWithImgStr:self.bottomArray[i] andTitle:self.bottomArray[i+6]];
        
        botView.layer.shadowOpacity = 0.05;
        
        [botView setBackgroundColor:[UIColor whiteColor]];
        
        [botView setTag:i];
        
        [bottomView addSubview:botView];
        
        // 添加 点击事件
        [botView addTarget:self action:@selector(didBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}

#pragma mark - 设置所有模块的 约束
- (void)setViewsMasonry {
    
    
#warning 到时的屏幕适配在这里设置
//MARK: - 到时的屏幕适配在这里设置
    
    // 天气
    [self.weatherView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.offset(64);  // 导航控制器 的 64
        make.left.right.offset(0);
        make.height.offset(80);
    }];
    
    // 图片轮播
    CGFloat height = [UIScreen mainScreen].bounds.size.width * 0.45;
    
    [self.scrollView.view mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.weatherView.mas_bottom);
        make.left.right.offset(0);
        make.height.offset(height);
        
    }];
    
    // 名医通
    [self.superDoctorView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.scrollView.view.bottom);
        make.left.right.offset(0);
        make.height.offset(50);
    }];
    
    // 底部 模块的 约束
    [self.buttomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.superDoctorView.mas_bottom);
        make.left.right.bottom.offset(0);
    }];
}

#pragma mark - 实现底部 6 个View 的点击事件方法
- (void)didBottomButton:(AkuBottomView *)sender {
    
    if (sender.tag < 5) {
        
        DiseaseViewController *dVC = [[DiseaseViewController alloc] init];
        
        dVC.title = self.bottomArray[sender.tag+6];
        dVC.ClickButtonNumer = (int)sender.tag;
        
        [self.navigationController pushViewController:dVC animated:NO];
    }else{
        
        WelfareViewController *pb = [[WelfareViewController alloc] init];
        pb.view.backgroundColor = [UIColor whiteColor];
        pb.title = self.bottomArray[sender.tag+6];
        
        [self.navigationController pushViewController:pb animated:NO];
    }
}

#pragma mark - viewDidLayoutSubviews更新约束  底部 6个子view 的约束
- (void)viewDidLayoutSubviews {
    
    [super viewDidLayoutSubviews];
    
    //MARK: - 更新 底部的 6 个 view 的 frame
    CGFloat margin = 3;
    NSInteger row = 3;
    NSInteger line = 2;
    CGFloat botWidth  = ( self.buttomView.bounds.size.width - (row + 1) * margin ) / row;
    CGFloat botHeight = ( self.buttomView.bounds.size.height - (line + 1) * margin ) / line;
    
    for (int i = 0; i < self.bottomArray.count / 2; i++) {
        
        AkuBottomView *botView = (AkuBottomView *)self.buttomView.subviews[i];
        
        CGFloat botX = ( botWidth + margin  ) * (i % 3) + margin;
        CGFloat botY = ( botHeight + margin ) * (i / 3) +margin;
        
        botView.frame = CGRectMake(botX, botY, botWidth, botHeight);
    }
    // ========================================
}

#pragma maek - 懒加载
- (NSArray *)bottomArray {
    
    if (!_bottomArray) {
        
        _bottomArray = @[@"zl",@"xx",@"sj",@"xy",@"gt",@"gy",
                         @"肿瘤",@"心血管",@"神经病",@"血液科",@"骨科",@"公益"];
    }
    return _bottomArray;
}

















@end
